import 'package:get/get.dart';
import 'package:sinosheets/mixin/refresh_mixin.dart';
import 'package:sinosheets/network/api.dart';

class CategoryProductLogic extends GetxController with RefreshMixin {
  String categoryId = '';
  String categoryName = '';

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    categoryId = Get.arguments['categoryId'];
    categoryName = Get.arguments['categoryName'];
    onRefresh();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  int page = 1;
  int pageSize = 20;
  @override
  Future<void> onRefresh() async {
    page = 1;
    await getCategoryProductList();
    super.onRefresh();
  }

  @override
  Future<void> onLoad() async {
    page++;
    await getCategoryProductList();
    super.onLoad();
  }

  ///获取分类商品列表
  List categoryProductList = [];
  Future<void> getCategoryProductList() async {
    Map<String, dynamic> params = {
      'id': categoryId,
      'pageNum': page,
      'pageSize': pageSize,
      'q': '',
      'code': '',
    };
    BaseResponse response = await ProductApi.getCategoryProductPageList(params);
    if (response.isSuccess) {
      if (page == 1) {
        categoryProductList = response.list ?? [];
      } else {
        categoryProductList.addAll(response.list ?? []);
      }
      update();
    }
  }
}
